package ControlLayer;
import DBLayer.*;
import ModelLayer.*;

public class SupplierCtr {

	private SupplierCtr(){
		
	}
	
	private  static SupplierCtr instance;
	
	/**
	 * find and return a supplier according to CVR number
	 */
	
	public Supplier findSupplier(String cvrNo)
	{
		DBSupplier dbsup = new DBSupplier();
		return dbsup.findSupplier(cvrNo, true);
	}
	
	/**
	 * deletes supplier from a database
	 */
	
	public void deleteSupplier(String cvrNo)
	{
		DBSupplier dbsup = new DBSupplier();
		
		dbsup.delete(new Supplier(cvrNo));
	}
	
	/**
	 * Creates a supplier with the given information
	 */
	
	public Supplier createSupplier(String cvrNo, String name, String address, String country, String phoneNo, String email)
	{
		Supplier supplier = new Supplier(cvrNo, name, address, country, phoneNo, email);
		
		DbConnection1.startTransaction();
		DBSupplier dbsup = new DBSupplier();
		
		try{
			dbsup.insertSupplier(supplier);
		}catch (Exception e){
			e.printStackTrace();
		}
		if(!DbConnection1.commitTransaction())
				DbConnection1.rollbackTransaction();
		
		return supplier;
	}
	
	public void updateSupplier(String cvrNo, String name, String address, String country, String phoneNo, String email)
	{
		Supplier supplier = new Supplier (cvrNo, name, address, country, phoneNo, email);
		DBSupplier dbsup = new DBSupplier();
		dbsup.updateSupplier(supplier);
		
	}
	
	public static SupplierCtr getInstance()
	{
		if(instance == null){
			instance = new SupplierCtr();
		}
		
		return instance;
	}
	
}//end class
